﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Model;
using ZyGames.DZPocker.Tools;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Common.Serialization;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 申请组局
	/// </summary>
	public class Action10029 : BaseAction
	{
		private int _gameUserId;
		private string _roomName = "斗牛房";
		//当前的财富值,这个要根据玩家ID来获得
		private int _currentTotalCoin = 0;
		//强制抓头功能
		private int _gripperCost = 2;
		//每手收费模式费用
		private int _vipPerWinnerPersonCost = 5;
		//每局收费费用
		private int _vipPerRoundCost = 2;
		//VIP收费最小百分比
		private int _vipMinPercent = 1;
		//最大百分比
		private int _vipMaxPercent = 5;
		//不允许旁观
		private int _canNotOnLookingCost = 2;

		private GameUser _currentGameUser = null;
		public Action10029(HttpGet actionGetter)
			: base((short)ActionType.DZApplyCreateRoom, actionGetter)
		{
		}

		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("GameUserId", ref _gameUserId))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public override bool TakeAction()
		{
			try
			{
				_currentGameUser = GameTable.Current.GetUser(_gameUserId);
				if (_currentGameUser == null)
				{
					ErrorCode = Language.Instance.ErrorCode;
				}
				//获得数据库一些服务费的相关配置
				this._currentTotalCoin = _currentGameUser.GameCoin;
				this._canNotOnLookingCost = ConfigEnvSet.GetInt("GameSetting.CanNotOnLookingCost", 2);
				this._vipPerWinnerPersonCost = ConfigEnvSet.GetInt("GameSetting.VIPPerWinnerPersonCost", 5);
				this._vipPerRoundCost = ConfigEnvSet.GetInt("GameSetting.VIPPerRondCost", 2);
				this._gripperCost = ConfigEnvSet.GetInt("GameSetting.GripperCost", 2);
				this._vipMinPercent = ConfigEnvSet.GetInt("GameSetting.VIPMinPercent", 1);
				this._vipMaxPercent = ConfigEnvSet.GetInt("GameSetting.VIPMaxPercent", 5);
			}
			catch (Exception ex)
			{
				Tool.WriteErrorLine("协议10029异常");
				return false;
			}
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(_roomName);
			this.PushIntoStack(_currentTotalCoin);
			this.PushIntoStack(Convert.ToBase64String(ProtoBufUtils.Serialize(new ShareCacheStruct<BindLevelCostsInfo>().FindAll())));
			this.PushIntoStack(Convert.ToBase64String(ProtoBufUtils.Serialize(new ShareCacheStruct<TimeLevelCostsInfo>().FindAll())));
			//强制抓头费用
			this.PushIntoStack(_gripperCost);
			this.PushIntoStack(_canNotOnLookingCost);
			//VIPCost结构
			this.PushIntoStack(Convert.ToBase64String(ProtoBufUtils.Serialize(new VIPCost() { 
				VIPPerWinnerPersonCost = this._vipPerWinnerPersonCost, 
				VIPPerRoundCost = this._vipPerRoundCost, 
				MinValue = this._vipMinPercent, 
				MaxValue = this._vipMaxPercent 
			})));
		}
	}
}
